<?php

namespace Console\Controller;

use Console\Common\Library\Page;

class TaskController extends CstBaseController
{
    private $taskLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->taskLogic = A('Console/Task', 'Logic');
    }

    public function taskList()
    {
        $typeDic = $this->taskLogic->getTypeDic();
        $this->assign("typeDic", $typeDic);
        $this->display();
    }


    public function editTask()
    {
        $id = I('get.id');
        if (empty($id)) {
            $this->sysErr("任务id不能为空");
        }

        $typeDic = $this->taskLogic->getTypeDic();

        $infoRes = $this->taskLogic->getInfo(array(
            "id" => $id
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->sysErr("未找到该任务");
        }

        $this->assign("typeDic", $typeDic);
        $this->assign("info", $info);
        $this->display();

    }

    /**
     * 获取我发布的任务列表
     */
    public function doGetTaskList()
    {
        $data = I('post.');
        $state = $data['state'];
        $page = $data['page'] ? $data['page'] : 1;
        $pageSize = 20;
        unset($data['page']);
        unset($data['state']);

        if ($state) {
            $now = date('Y-m-d H:i:s');
            switch ($state) {
                case 1:
                    $data['begin_time'] = array("EGT", $now);
                    break;
                case 2:
                    $data['begin_time'] = array("ELT", $now);
                    $data['end_time'] = array("EGT", $now);
                    break;
                case 3:
                    $data['end_time'] = array("ELT", $now);
                    break;
            }
        }

        $listRes = $this->taskLogic->getList($page, $pageSize, $data);
        $data = $listRes['Result'];
        $total = $data['total'];
        $list = $data['list'];

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $typeDic = $this->taskLogic->getTypeDic();

        $this->assign("typeDic", $typeDic);
        $this->assign("total", $total);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("pageHtml", $pageHtml);
        $html = $this->fetch('doGetTaskList');

        $res = array(
            "list" => $list,
            "html" => $html,
            "total" => $total,
            "pageTotal" => $pageTotal
        );
        $this->ajaxReturn(static::$res->suc("成功", $res));
    }


    /**
     * 编辑一个任务
     */
    public function doEditTask()
    {
        $data = I('post.');
        $id = $data['id'];
        unset($data['id']);

        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("任务ID不能为空"));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->taskLogic->getInfo(array(
            "id" => $id
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->ajaxReturn(static::$res->err("任务未找到"));
        }

        $res = $this->taskLogic->editTask($userId, $id, $data);
        $this->ajaxReturn($res);
    }

    /**
     * 删除任务
     */
    public function doDelTask()
    {
        $id = I('post.id');
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->taskLogic->getInfo(array(
            "id" => $id
        ));
        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->ajaxReturn(static::$res->err("任务未找到"));
        }

        $res = $this->taskLogic->delTask($userId, $id, "系统管理员删除");
        $this->ajaxReturn($res);
    }

}